package DataBase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import static java.lang.System.out;

/**
 * Created by Administrator on 2016/12/26 0026.
 */
public class DB {
    public static Connection con ;//澹版槑Connection鍙橀噺
    static String driver = "com.mysql.jdbc.Driver";
    static int i=1;

    public static void db()throws Exception{
        try {
            Class.forName("com.mysql.jdbc.Driver"); //加载mysq驱动
            String url =
                    "jdbc:mysql://localhost/html_data?user=root&password&useUnicode=true&&characterEncoding=gb2312&useSSL=false";//简单写法：url = "jdbc:myqsl://localhost/test(数据库名)? user=root(用户)&password=yqs2602555(密码)";
            String user = "root";
            String password = "";
            con = DriverManager.getConnection(url, user, password);

        } catch(ClassNotFoundException ex){
            //异常处理
            out.println("Sorry,can`t find the Driver!");
            ex.printStackTrace();
        }catch(SQLException ex){
            //数据库处理失败异常
            ex.printStackTrace();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            out.println("数据库数据成功获取！！");
        }
    }
    public static void insert_av(int av_id,String url,String title,String author,String categories,int coin,int favorites,int view)throws Exception{
        PreparedStatement psql = con.prepareStatement("insert into av_html values (?,?,?,?,?,?,?,?)");
        psql.setInt(1,av_id);
        psql.setString(2,url);
        psql.setString(3,title);
        psql.setString(4,author);
        psql.setString(5,categories);
        psql.setInt(6,coin);
        psql.setInt(7,favorites);
        psql.setInt(8,view);
        psql.executeUpdate();
        psql.close();

    }
    public static void insert_html(String url,String comments)throws Exception{
        PreparedStatement psql = con.prepareStatement("insert into html values (?,?,?)");
        psql.setInt(1,i);i++;
        psql.setString(2,url);
        psql.setString(3,comments);
        psql.executeUpdate();
        psql.close();

    }
    public static void close()throws Exception{
        con.close();
    }
}
